package com.itlong.cloud.controller.app;

import com.itlong.cloud.POJO.DTO.operate.OperateAppFunctionGetPageDTO;
import com.itlong.cloud.POJO.PO.AppFunctionInfoPO;
import com.itlong.cloud.POJO.PO.AppPackagePO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.app.AppFunctionInfoVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.noncore.operate.IOperateAppFunctionInfoServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营-App管理-功能管理控制器
 * </desc>
 *
 * @createDate 2017/10/13
 */
@RestController
@RequestMapping("/appManage/function")
public class OperateAppFunctionController extends BaseController{
    @Autowired
    IOperateAppFunctionInfoServiceClient iOperateAppFunctionInfoServiceClient;

    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param dto 查询参数DTO
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/13
     */
    @AuthAccessRequired
    @RequestMapping(path = "getPage",method = RequestMethod.POST)
    @ResponseBody
    public Object getAppFunctionPage(OperateAppFunctionGetPageDTO dto){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateAppFunctionGetPageDTO.class,request);
            Page<AppFunctionInfoPO> page=iOperateAppFunctionInfoServiceClient.getPage(dto);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_APP管理_功能管理getPage】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      添加功能
     * </desc>
     *
     * @param appFunctionInfoPO 功能实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/13
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.APP_FUNCTION_LOG_SAVE)
    @RequestMapping(path = "save",method = RequestMethod.POST)
    @ResponseBody
    public Object saveAppFunction(AppFunctionInfoPO appFunctionInfoPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appFunctionInfoPO= HttpProtocolUtil.parseRequestParamToDTO(AppFunctionInfoPO.class,request);
            iOperateAppFunctionInfoServiceClient.save(appFunctionInfoPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_APP管理_功能管理save】添加功能失败",e);
        }
        return messageVO;

    }

    /**
     * <desc>
     *      修改功能
     * </desc>
     *
     * @param appFunctionInfoPO 功能实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/13
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.APP_FUNCTION_LOG_UPDATE)
    @RequestMapping(path = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object updateAppFunction(AppFunctionInfoPO appFunctionInfoPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appFunctionInfoPO= HttpProtocolUtil.parseRequestParamToDTO(AppFunctionInfoPO.class,request);
            iOperateAppFunctionInfoServiceClient.update(appFunctionInfoPO);
            messageVO=new MessageVO(appFunctionInfoPO.getAppFunctionId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-_APP管理_功能管理update】更新功能失败", e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      查询单条功能
     * </desc>
     *
     * @param appFunctionInfoPO 功能实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/13
     */
    @RequestMapping(path = "getOne",method = RequestMethod.POST)
    @ResponseBody
    public Object getAppFunction(AppFunctionInfoPO appFunctionInfoPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appFunctionInfoPO= HttpProtocolUtil.parseRequestParamToDTO(AppFunctionInfoPO.class,request);
            appFunctionInfoPO=iOperateAppFunctionInfoServiceClient.getOne(appFunctionInfoPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),appFunctionInfoPO);
        }catch (Exception e){
            throw new DataAccessException("【运营_APP管理_功能管理getOne】获取单条功能失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      删除功能
     * </desc>
     *
     * @param appappFunctionIds 功能id数组字符串 ，号分隔
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/13
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.APP_FUNCTION_LOG_DELETE)
    @RequestMapping(path = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteAppFunction(String appappFunctionIds){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appappFunctionIds=request.getAttribute("appFunctionIds")==null?null:request.getAttribute("appFunctionIds").toString();
            iOperateAppFunctionInfoServiceClient.delete(appappFunctionIds);
            messageVO=new MessageVO(appappFunctionIds,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch(Exception e){
            throw new DataAccessException("【运营_APP管理_功能管理delete】删除功能失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      查询下级节点
     * </desc>
     *
     * @param appFunctionId 功能id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/13
     */
    @RequestMapping(path = "getChilds",method = RequestMethod.POST)
    @ResponseBody
    public Object getAppFunctionChilds(String appFunctionId){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appFunctionId=request.getAttribute("appFunctionId")==null?null:request.getAttribute("appFunctionId").toString();
            List<AppFunctionInfoVO> list=iOperateAppFunctionInfoServiceClient.getChilds(appFunctionId);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_APP管理_功能管理getChilds】查询下级节点失败",e);
        }
        return messageVO;
    }


    /**
     * <desc>
     *      启用或禁用
     * </desc>
     *
     * @param appFunctionInfoPO 功能实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/26
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.APP_FUNCTION_LOG_USEORBAN)
    @RequestMapping(path = "banOrUse",method = RequestMethod.POST)
    @ResponseBody
    public Object banOrUseAppFunction(AppFunctionInfoPO appFunctionInfoPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appFunctionInfoPO= HttpProtocolUtil.parseRequestParamToDTO(AppFunctionInfoPO.class,request);
            AppFunctionInfoPO oldPo=iOperateAppFunctionInfoServiceClient.getOne(appFunctionInfoPO);
            if(oldPo.getAllocation()==0){
                return new MessageVO(OperateErrorCodeEnum.APP_FUNCTION_ERROR.getErrorCode());
            }
            iOperateAppFunctionInfoServiceClient.update(appFunctionInfoPO);
            messageVO=new MessageVO(appFunctionInfoPO.getAppFunctionId(), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_APP管理_功能管理banOrUse】启用或禁用失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取分配菜单所有节点
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/26
     */
    @RequestMapping(path = "getAllotFunction",method = RequestMethod.POST)
    @ResponseBody
    public Object getAllotAppFunction(AppPackagePO appPackagePO){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appPackagePO= HttpProtocolUtil.parseRequestParamToDTO(AppPackagePO.class,request);
            List<AppFunctionInfoVO> list=iOperateAppFunctionInfoServiceClient.getAllotFunction(appPackagePO.getAppPackageId());
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_APP管理_功能管理getAllotFunction】获取分配菜单所有节点失败");
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取当前节点的父级节点以及同级别所有父级节点
     * </desc>
     *
     * @param parent 功能id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @RequestMapping(path = "getParents",method = RequestMethod.POST)
    @ResponseBody
    public Object getParentLevelFunctions(String parent){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            parent=request.getAttribute("parent")==null?null:request.getAttribute("parent").toString();
            List<AppFunctionInfoPO> list=iOperateAppFunctionInfoServiceClient.getParentLevelFunctions(parent);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_APP管理_功能管理getParentLevelFunctions】获取当前节点的父级节点以及同级别所有父级节点失败");
        }
        return messageVO;
    }

    /**
     * <desc>
     *      云半径多场景项目上线处理旧数据
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/5/12
     */
    @UnParamEncrypt
    @UnAuthAccessRequired
    @PostMapping("handlerOldPackage")
    public Object handlerOldPackage(){
        try {
            Integer result = iOperateAppFunctionInfoServiceClient.handlerOldPackage();
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), result);
        }catch (Exception e){

        }
       return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode());
    }
}
